package com.infnoon.parking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.infnoon.parking.common.ErrorCode;
import com.infnoon.parking.exception.BusinessException;
import com.infnoon.parking.model.entity.Vehicles;
import com.infnoon.parking.service.VehiclesService;
import com.infnoon.parking.mapper.VehiclesMapper;
import org.springframework.stereotype.Service;

/**
* @author 31124
* @description 针对表【vehicles】的数据库操作Service实现
* @createDate 2024-12-16 14:54:54
*/
@Service
public class VehiclesServiceImpl extends ServiceImpl<VehiclesMapper, Vehicles>
    implements VehiclesService {
    @Override
    public void saveVehicles(String carnumber) {
        // 首先 检查数据库表 Vehicles 中 此车有无记录
        // 如果 此车已在数据库表 Vehicles 中， 无需添加 或 报错，直接 return
        QueryWrapper<Vehicles> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("carnumber", carnumber);
        long count = this.baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            return ;
        }

        // 同理 通过 @Builder 注解 去实现 省时便捷又安全 的赋值
        Vehicles vehicles = Vehicles.builder().
                carnumber(carnumber).
                car_Type("DEFAULT_TYPE").
                manufacturer("DEFAULT_MANUFACTURER").
                car_long(220.0).
                car_wide(85.0).
                build();

        boolean saveResult = this.save(vehicles);
        if (!saveResult) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "停车失败，数据库错误");
        }
    }
}




